package com.jz.community.moduleauthorization.login.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.CommUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.commview.view.widget.LoginButton;
import com.jz.community.moduleauthorization.R;
import com.jz.community.moduleauthorization.R2;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.moduleauthorization.login.presenter.LoginPresenter;
import com.jz.community.moduleauthorization.utils.LoginEmptyUtils;
import com.jz.community.basecomm.utils.TimeUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Tony.Fan on 2018/3/22 09:33
 */
@Route(path = RouterIntentConstant.MODULE_AUTHORIZATION_RESET_PWD)
public class LoginResetPwdActivity extends BaseMvpActivity<LoginView.View, LoginPresenter> implements LoginView.View {
    @BindView(R2.id.login_phone_et)
    EditText loginPhoneEt;
    @BindView(R2.id.reset_cod_bt)
    Button resetCodBt;
    @BindView(R2.id.login_code_et)
    EditText loginCodeEt;
    @BindView(R2.id.login_reset_pwd_et)
    EditText loginResetPwdEt;
    @BindView(R2.id.login_confirm_reset_pwd_et)
    EditText loginConfirmResetPwdEt;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.reset_btn)
    LoginButton reset_btn;
    private TimeUtils timeUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        setImmersionBar(titleToolbar);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_login_reset_pwd_layout;
    }

    @Override
    protected void initDatas() {
        initTitle("忘记密码", "");
        reset_btn.setText("确定");
    }


    @Override
    protected void addListener() {
        super.addListener();
        reset_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmResetClick(v);
            }
        });
    }

    //获取验证码
    public void resetCodeClick(View view) {
        String loginPhone = loginPhoneEt.getText().toString().trim();
        if (LoginEmptyUtils.isLoginPhone(this, loginPhone)) {
            mPresenter.getLoginSmsCode("2", loginPhone);
        }
    }

    //重置密码
    public void confirmResetClick(View view) {
        CommUtils.hideKeyboard(view, this);
        String tel = loginPhoneEt.getText().toString().trim();
        String code = loginCodeEt.getText().toString().trim();
        String password = loginResetPwdEt.getText().toString().trim();
        String password_again = loginConfirmResetPwdEt.getText().toString().trim();
        if (LoginEmptyUtils.isHasPwdEmpty(this, tel, code, password, password_again)) {
            mPresenter.getResetPwdInfo(mapParam(tel, password));
        }
    }

    private HashMap<String, Object> mapParam(String tel, String pwd) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", tel);
        map.put("password", pwd);
        return map;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void showLoginInfo(LoginInfo loginInfo) {
    }

    @Override
    public void showSmsCodeInfo(LoginInfo baseResponseInfo) {
        handelSmsCodeState(baseResponseInfo);
    }

    /**
     * 验证码结果处理状态
     *
     * @param baseResponseInfo
     */
    private void handelSmsCodeState(LoginInfo baseResponseInfo) {
        if (!Preconditions.isNullOrEmpty(baseResponseInfo.getVcode())) {
            if (timeUtils == null) {
                WpToast.l(this, "验证码发送成功");
                timeUtils = new TimeUtils(120 * 1000, 1000, LoginResetPwdActivity.this, resetCodBt);
            }
            timeUtils.start();
        } else {
            WpToast.l(this, baseResponseInfo.getMessage());
            refreshRegainBackground();
        }
    }

    @Override
    public void showResetPwdInfo(BaseResponseInfo userInfo) {
        if (!Preconditions.isNullOrEmpty(userInfo)) {
            WpToast.l(this, "重置成功");
            finish();
        } else {
            WpToast.l(this, "重置失败");
            refreshRegainBackground();
        }

    }

    @Override
    public void showError() {
        refreshRegainBackground();
    }

    /**
     * 刷新按钮
     */
    private void refreshRegainBackground() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                reset_btn.regainBackground("确定");
            }
        }, 1000);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtils != null) {
            timeUtils.cancel();
        }
    }
}
